<?php
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : '';
$active = 'member';

//注册
if ($op == 'register') {
    $html_title = '会员注册';
    if (is_post()) {
        try {
            $data['mobile'] = input('mobile');
            $data['password'] = input('password');
            $data['password1'] = input('password1');
            if (cmf_check_mobile($data['mobile']) != true) {
                ajaxError('请输入正确的手机号');
            }
            $userExists = pdo_get('yq_haircircle_users', array('mobile' => $data['mobile']));
            if (!empty($userExists)) {
                ajaxError('手机号已被注册');
            }
            if (empty($data['password']) || strlen($data['password'] < 6)) {
                ajaxError('请填写长度大于6的密码');
            }
            if ($data['password'] != $data['password1']) {
                ajaxError('两次密码输入不一致');
            }
            $pid = isset($_GPC['pid']) ? intval($_GPC['pid']) : 0;
            $userData = [
                'pid' => $pid,
                'user_pass' => cmf_password($data['password']),
                'user_status' => 1,
                'mobile' => $data['mobile'],
                'create_time' => time(),
                'vip_end_time' => time() + 3600 * 24 * 365
            ];

            pdo_begin();

            $register = pdo_insert('yq_haircircle_users', $userData);
            if (empty($register)) {
                pdo_rollback();
                ajaxError('注册失败');
            }
            $user_id = pdo_insertid();

            $url = 'http://' . $_SERVER['HTTP_HOST'] . '/app/' . $this->createMobileUrl('public', array('op' => 'register', 'pid' => $user_id));
            $tuiguang_setting = cmf_get_option('tuiguang_setting');
            if(isset($tuiguang_setting['thumb1']) && !empty($tuiguang_setting['thumb1'])){
                $bg = '../attachment/' . cmf_asset_relative_url($tuiguang_setting['thumb1']);
            }else{
                $bg = '../addons/yq_haircircle/qrcode-background.png';
            }
            $qrcode = cmf_get_qrcode($url,251,$bg);
            if (!$qrcode) {
                pdo_rollback();
                ajaxError('更新信息失败，更新二维码失败');
            }
            $res = pdo_update('yq_haircircle_users', array('user_login' => $user_id, 'qrcode' => $qrcode), array('id' => $user_id));
            if (empty($res)) {
                pdo_rollback();
                ajaxReturn('注册失败');
            }

            pdo_commit();

            $user_info = pdo_get('yq_haircircle_users', array('id' => $user_id));
            session('user_id', $user_id);
            session('user', $user_info);
            ajaxSuccess('注册成功');
        } catch (\Exception $e) {
            ajaxError('出错啦：' . $e->getMessage());
        }
    }
    $pid = isset($_GPC['pid']) ? intval($_GPC['pid']) : 0;
    include $this->template('public_register');
    exit;
}
//登录
if ($op == 'login') {
    $html_title = '会员登录';
    if (is_post()) {
        $data['mobile'] = input('mobile');
        $data['password'] = input('password');
        if (empty($data['mobile'])) {
            ajaxError('请输入手机号');
        }
        $findUser = pdo_get('yq_haircircle_users', array('mobile' => $data['mobile']));
        if (empty($findUser)) {
            ajaxError("该用户不存在!");
        }

        if (empty($data['password'])) {
            ajaxError('请输入密码');
        }

        if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
            ajaxError("密码不正确!");
        }
        switch ($findUser['user_status']) {
            case 0:
                ajaxError('您已被拉黑!');
            case 2:
                ajaxError('账户还没有验证成功!');
            case 3:
                ajaxError('请联系客服审核');
        }
        $results = $findUser;
        $results['last_login_ip'] = get_client_ip(0, true);
        $results['last_login_time'] = time();
        $results['openid'] = isset($_GPC['openid']) ? $_GPC['openid'] : '';
        $results['user_nickname'] = isset($_GPC['user_nickname']) ? $_GPC['user_nickname'] : '';
        $results['sex'] = isset($_GPC['sex']) ? $_GPC['sex'] : '';
        $results['avatar'] = isset($_GPC['avatar']) ? $_GPC['avatar'] : '';
        pdo_update('yq_haircircle_users', $results, array('id' => $findUser['id']));

        session('user_id', $findUser['id']);
        session('user', $results);
        ajaxSuccess("登录成功!");
    }
    if (session('user_id')) {
        redirect($this->createMobileUrl('index', array('op' => 'index')));
    }
    $file = IA_ROOT . '/addons/' . ModuleName() . '/api/WeChat/WeChatAPI.api.php';
    if (is_file($file)) {
        require_once $file;
    } else {
        message('系统错误');
    }
    $wxset = pdo_get('account_wechats',array('uniacid'=>$_W['uniacid']));
    if (isset($_GET['code']) && $_GET['code']) {
        //第二次鉴权
        $_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        if ($_GET['code'] != 'authdeny') {
            //用户授权
            $options['appid'] = $wxset['key'];
            $options['appsecret'] = $wxset['secret'];
            $wx = new Wechat($options);
            $wechat_userinfo = $wx->getOauthAccessToken($_GET['code']);//获取access_token和openid
            $fansinfo = $wx->getUserInfo($wechat_userinfo['openid']);
            if(empty($fansinfo)){
                message('未获取到微信用户信息');
            }
        } else {
            message('本应用需要您的授权才可以使用!');
        }
    } else {
        $_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $options['appid'] = $wxset['key'];
        $options['appsecret'] = $wxset['secret'];
        $wx = new Wechat($options);
        $squrl = $wx->getOauthRedirect($_url, '1', 'snsapi_base');
        header("Location:" . $squrl);
    }
    include $this->template('public_login');
    exit;
}
//退出登录
if ($op == 'logout') {
    session('user_id', null);
    session('user', null);
    redirect($this->createMobileUrl('index', array('op' => 'index')));
}
//修改密码
if ($op == 'forgot') {
    $html_title = '修改密码';
    include $this->template('public_forgot');
    exit;
}
//客服中心
if ($op == 'kefu') {
    $html_title = '客服中心';
    $site_setting = cmf_get_option('site_setting');
    include $this->template('public_kefu');
    exit;
}